//
//  AY_BookSettingPage.m
//  AiYue
//
//  Created by boWen on 2023/10/19.
//

#import "AY_BookSettingPage.h"
#import "AY_BookSettingCell.h"
#import "AY_BookUpdatePage.h"
@interface AY_BookSettingPage ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * baseTableView;
@end

@implementation AY_BookSettingPage

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithNav];
}

- (void)initWithNav {
    self.gk_navTitle = @"设置";
    [self.view addSubview:self.baseTableView];
}


- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT  - GK_TABBAR_HEIGHT - GK_STATUSBAR_NAVBAR_HEIGHT)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.scrollEnabled = NO;
        [myTableView registerClass:[AY_BookSettingCell class] forCellReuseIdentifier:@"AY_BookSettingCell"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}


#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
        return section == 0 ? 1 : 3;
    }else{
        return section == 0 ? 1 : 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookSettingCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookSettingCell"];
    [cell setData:indexPath];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(65);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return YNUM(16);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        AY_BookUpdatePage * page = [AY_BookUpdatePage new];
        page.userInfoModel = self.userInfoModel;
        [self.navigationController pushViewController:page animated:YES];
    }else {
        BOOL isShenHe = [AYMMKV getBoolForKey:ISSHENHE defaultValue:YES];
        int huancunRow= isShenHe?1:2;
        if (indexPath.row == huancunRow) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"提示"                                            message: @"确定要清除缓存？" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [[SDWebImageManager sharedManager].imageCache clearWithCacheType:SDImageCacheTypeAll completion:^{
                    ShowToast(@"清除缓存成功");
                }];
            }];
            // KVC修改字体颜色
            [sureAction setValue:COLOR(@"FF5443") forKey:@"_titleTextColor"];
            [alertController addAction:sureAction];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [cancelAction setValue:COLOR(@"999999") forKey:@"_titleTextColor"];
            [alertController addAction:cancelAction];
            
            [self presentViewController: alertController animated: YES completion: nil];
        }else if (indexPath.row == (huancunRow+1)) {
            ShowToast(@"当前已是最新版本");
        }
    }
}

@end
